package com.hoshiicloud.user.controller.visitors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.visitors.Visitors;
import com.hoshiicloud.user.param.visitors.VisitorsAddParam;
import com.hoshiicloud.user.param.visitors.VisitorsQueryParam;
import com.hoshiicloud.user.param.visitors.VisitorsUpdateParam;
import com.hoshiicloud.user.service.visitors.IVisitorsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author:campchen
 * @Date:Created in 下午 05:15 2019/9/10
 * @PACKAGENAME com.hoshiicloud.user.controller.visitors
 * @Description
 */
@Slf4j
@RestController
@RequestMapping("/visitors/visitors")
@Api(value = "访客记录", tags = {"访客记录"})
@ApiSort(10)
public class VisitorsController {

    @Autowired
    private IVisitorsService visitorsService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询访客记录
     */
    @ApiOperation(value = "分页查询访客记录")
    @PostMapping("/getPage")
    public Response page(@RequestBody @Valid VisitorsQueryParam param) {
        try {
            QueryWrapper<Visitors> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(StringUtils.isNotBlank(param.getAddress()), Visitors::getAddress, param.getAddress())
                    .eq(StringUtils.isNotBlank(param.getMac()), Visitors::getMac, param.getMac())
                    .eq(StringUtils.isNotBlank(param.getUserCode()), Visitors::getUserCode, param.getUserCode())
                    .eq((param.getVisitTime() != null), Visitors::getVisitTime, param.getVisitTime())
                    .eq((param.getPlatformId() != null && param.getPlatformId() != 0), Visitors::getPlatformId, param.getPlatformId())
                    .eq((param.getStoreId() != null), Visitors::getStoreId, param.getStoreId())
                    .eq((param.getChannel() != null), Visitors::getChannel, param.getChannel())
                    .eq(StringUtils.isNotBlank(param.getCreatedBy()), Visitors::getCreatedBy, param.getCreatedBy())
                    .eq((param.getCreatedTime() != null), Visitors::getCreatedTime, param.getCreatedTime())
                    .eq(StringUtils.isNotBlank(param.getUpdatedBy()), Visitors::getUpdatedBy, param.getUpdatedBy())
                    .eq((param.getUpdatedTime() != null), Visitors::getUpdatedTime, param.getUpdatedTime());
            Page<Visitors> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            //return Response.data(visitorsService.getPage(page, queryWrapper, key));
            return Response.data("");
        } catch (NumberFormatException e) {
            log.error("分页查询访客记录异常", e);
            return Response.failed("分页查询访客记录异常");
        } catch (Exception e1) {
            log.error("分页查询访客记录异常", e1);
            return Response.failed(e1.getMessage());
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询访客记录
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询访客记录")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(visitorsService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增访客记录")
    public Response save(@RequestBody @Valid VisitorsAddParam param) {
        Visitors data;
        try {
            data = BeanUtils.covertType(param, Visitors.class);
        } catch (Exception e) {
            log.error("新增访客记录Bean转换异常", e);
            return Response.failed("新增访客记录Bean转换异常");
        }
        //return Response.data(visitorsService.insert(data));
        return Response.data("");
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改访客记录
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改访客记录")
    public Response update(@RequestBody @Valid VisitorsUpdateParam param) {
        Visitors data;
        try {
            data = BeanUtils.covertType(param, Visitors.class);
        } catch (Exception e) {
            log.error("修改访客记录Bean转换异常", e);
            return Response.failed("修改访客记录Bean转换异常");
        }
        return Response.data(visitorsService.updateById(data));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除访客记录
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除访客记录")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        //boolean flag = visitorsService.deleteById(id);
        boolean flag = true;
        if (flag) {
            return Response.data("删除访客记录成功");
        } else {
            return Response.data("删除访客记录失败");
        }
    }


    @ApiOperation(value = "批量查询访客数信息")
    @PostMapping("/queryBatch")
    public Response queryBatch(@RequestBody List<OmnipotentWrapper> param) {
        return Response.data(visitorsService.queryBatch(param));
    }
}
